package fei.yuan.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import fei.yuan.DB.MySQLiteDatabase;
import fei.yuan.homeaccountbook.R;
import fei.yuan.model.AccountDetailData;
import fei.yuan.utils.Constant;
import fei.yuan.utils.DateToolClass;


public class HomeaccountbookActivity extends Activity {
	/** Called when the activity is first created. */
	// private EditText factorOne;
	// private EditText factorTwo;
	private TextView textViewDate;
	private Button btnSet;         //设置
	private Button btnAddDetail;
	private Button btnSetBudget;   //设置预算
	private ListView lvDetail;

	// 存储数据的数组列表
	ArrayList<HashMap<String, Object>> listData;
	// 适配器
	SimpleAdapter listItemAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		LinearLayout temp = (LinearLayout) findViewById(R.id.linearlayoutBack);
		/*
		 * temp.setBackgroundDrawable(getResources().getDrawable(
		 * R.drawable.background9)); temp.setPadding(0, 0, 0, 0);
		 */

		// Drawable d=Drawable.createFromPath(R.drawable.background9);
		// temp.setBackgroundDrawable(d);

		//
		// 根据控件的ID取得代表控件的对像	
		textViewDate = (TextView) findViewById(R.id.date);
		btnSet = (Button) findViewById(R.id.setButton);
		btnAddDetail = (Button) findViewById(R.id.addBtn);
		lvDetail = (ListView) findViewById(R.id.lv);
		btnSetBudget = (Button) findViewById(R.id.setBudgetButton);
		

		// 取得手机的分辨率
		DisplayMetrics dm = GetPhonePx();
		int phoneWidth = dm.widthPixels;
		int phoneHeight = dm.heightPixels;
        //设置listview的高度	
		int fixheight = 60 + 260 + 60 + 60;
		int lv_height = phoneHeight - fixheight;
	        lvDetail.setLayoutParams(new LinearLayout.LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT, lv_height, 1f));

		System.out.println("phoneWidth---------->" + phoneWidth);
		System.out.println("phoneHeight---------->" + phoneHeight);
		
		System.out.println("Constant.DATE_NOW---------->" + Constant.DATE_NOW);
		System.out.println("Constant.TIME_NOW---------->" + Constant.TIME_NOW);

		//Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		textViewDate.setText(sdf.format(Constant.TODAY) + "   " + DateToolClass.GetWeekDay(Constant.TODAY));

		// 为ListView添加数据源START 从SQLite数据库中读取数据
		MySQLiteDatabase mySQLiteDb = MySQLiteDatabase.getInstance(this);

		listData = mySQLiteDb.getAllDataByDate(Constant.DATE_NOW);

		System.out.println("listData--------->" + listData);

		listItemAdapter = new SimpleAdapter(this, listData,// 数据来源
				R.layout.item,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项			
				new String[] { "CATEGORYPICTURE", "ITEMNAME", "PHONEPICTURE","AMOUNT" },
				// ImageItem的XML文件里面的项目			
				new int[] { R.id.ItemImage, R.id.ItemName,R.id.ItemPicture, R.id.ItemAmount });

		lvDetail.setAdapter(listItemAdapter);
		// lvDetail.setOnCreateContextMenuListener(listviewLongPress);
		
       //为ListView添加单击事件
		lvDetail.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {  
                       		
        		//跳转到设置画面      		
        		Intent intent = new Intent();            	
        		//点击的行数传到下一个画面
        		intent.putExtra("itemRow", arg2);
    			intent.setClass(HomeaccountbookActivity.this,
    					AddPaymentDetail.class);
    			HomeaccountbookActivity.this.startActivity(intent);       		                   
            }
        });
		
		// 将监听器绑定到按钮对象上	
		btnSet.setOnClickListener(new SetListener());
		btnAddDetail.setOnClickListener(new AddDetailListener());
		btnSetBudget.setOnClickListener(new SetBudgetListener());

	}

	// 取得手机的分辨率，单位px
	private DisplayMetrics GetPhonePx() {
		// TODO Auto-generated method stub
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm;
	}

	// 为【设置】按钮添加事件开始
	class SetListener implements android.view.View.OnClickListener {

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(HomeaccountbookActivity.this, SetForm.class);
			HomeaccountbookActivity.this.startActivity(intent);
		}

	}

	// 为【设置】按钮添加事件结束
	// 为【添加+】按钮添加事件开始
	class AddDetailListener implements android.view.View.OnClickListener {

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			//点击的行数传到下一个画面 新添加的场合 行数设置为-1
    		intent.putExtra("itemRow", -1);
			intent.setClass(HomeaccountbookActivity.this,
					AddPaymentDetail.class);
			HomeaccountbookActivity.this.startActivity(intent);
		}

	}
	// 为【添加+】按钮添加事件结束
	
	
	// 为【设置预算】按钮添加事件开始
		class SetBudgetListener implements android.view.View.OnClickListener {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(HomeaccountbookActivity.this, SetBudget.class);
				HomeaccountbookActivity.this.startActivity(intent);
			}

		}
// 为【设置预算】按钮添加事件结束
	

}